/*******************************************************************************
File    : exti.c
Purpose : 
********************************** Includes ***********************************/
#include "port.h"
#include "motionapp.h"

/********************************* Defines ************************************/

#define INVEN_INT_PIN                         GPIO_Pin_5
#define INVEN_INT_GPIO_PORT                   GPIOB
#define INVEN_INT_GPIO_CLK                    RCC_APB2Periph_GPIOB
#define INVEN_INT_EXTI_PORT                   GPIO_PortSourceGPIOB
#define INVEN_INT_EXTI_PIN                    GPIO_PinSource5
#define INVEN_INT_EXTI_LINE                   EXTI_Line5

/**********************************Globals ************************************/
/********************************* Prototypes *********************************/

/***********************************Functions *********************************/
void InvInterruptInit(void)
{
	GPIO_InitTypeDef   GPIO_InitStructure;
	EXTI_InitTypeDef   EXTI_InitStructure;

	RCC_APB2PeriphClockCmd(INVEN_INT_GPIO_CLK|RCC_APB2Periph_AFIO, ENABLE); 

	/* Configure invensense sensor interrupt pin as input floating */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = INVEN_INT_PIN;
	GPIO_Init(INVEN_INT_GPIO_PORT, &GPIO_InitStructure); 

	/* Connect EXTI Line to inv sensor interrupt pin */
	GPIO_EXTILineConfig(INVEN_INT_EXTI_PORT, INVEN_INT_EXTI_PIN);

	/* Configure EXTI Linex */
	EXTI_InitStructure.EXTI_Line = INVEN_INT_EXTI_LINE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);

}

void EnableInvInterrupt(void)
{
	EXTI_InitTypeDef   EXTI_InitStructure;
	/* Configure EXTI Line1 */
	EXTI_InitStructure.EXTI_Line = INVEN_INT_EXTI_LINE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
}
void DisableInvInterrupt(void)
{
	EXTI_InitTypeDef   EXTI_InitStructure;
	/* Configure EXTI Line1 */
	EXTI_InitStructure.EXTI_Line = INVEN_INT_EXTI_LINE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = DISABLE;
	EXTI_Init(&EXTI_InitStructure);
	EXTI_ClearITPendingBit(INVEN_INT_EXTI_LINE);
}

void EXTI9_5_IRQHandler(void)
{
	if(SET==EXTI_GetITStatus(INVEN_INT_EXTI_LINE)){		
		gyro_data_ready_cb();
		 /* Clear the EXTI line x pending bit */
		EXTI_ClearITPendingBit(INVEN_INT_EXTI_LINE);
	}
}

